<template>
<!-- 行政材料 -->
  <div class="module_">
    <div class="flex-space-between mb8">
      <!-- <el-form :inline="true" :model="pageparm" class="search_box">
        <el-form-item label="搜索：">
          <el-input
            size="small"
            v-model="pageparm.patient"
            placeholder="患者姓名搜索"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            size="small"
            v-model="pageparm.prescribe_doctor"
            placeholder="开单医生姓名搜索"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            size="small"
            v-model="pageparm.execute_doctor"
            placeholder="执行医生姓名搜索"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            @change="dateSearch"
            v-model="pageparm.charging_time"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
            type="date"
            placeholder="收费时间搜索">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-search"
            @click="search"
            >搜索</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button size="small" @click="clearSearch">重置</el-button>
        </el-form-item>
      </el-form> -->
      <p></p>
      <el-button size="small" type="primary" @click="handle(null, {},3)">添加</el-button>
    </div>
    <el-table
      :data="tableData"
      class="flex-1"
      height="150"
      size="small"
      border
      highlight-current-row
      v-loading="tableLoading"
      @sort-change="tableSortChange"
      stripe>
      <el-table-column
        align="center"
        prop="sub_department.name"
        label="二级科室"
        width="150"
        sortable="custom"
        show-overflow-tooltip>
      </el-table-column>
      <el-table-column
        align="center"
        prop="total_amount"
        label="汇总金额"
        width="200"
        sortable="custom"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        align="center"
        prop="date"
        label="日期"
        width="150"
        sortable="custom"
        show-overflow-tooltip
      >
      </el-table-column>
      <!-- <el-table-column
        align="center"
        prop="material_type"
        label="材料类型"
        width="150"
        sortable="custom"
        show-overflow-tooltip>
      </el-table-column> -->
      
      <el-table-column align="center" label="操作" width="150">
        <template slot-scope="scope">
          <el-tooltip  effect="dark" content="修改信息" placement="bottom-end">
            <el-button size="mini"  @click="handle(scope.$index, scope.row,2)" icon="el-icon-edit" circle></el-button>
          </el-tooltip>
          <el-tooltip  effect="dark" content="删除" placement="bottom-end">
            <el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)" icon="el-icon-delete" circle></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <Pagination
      v-bind:child-msg="pageparm"
      @callFather="callFather"
    ></Pagination>

    <el-dialog
      class="dialog"
      :visible.sync="editFormVisible"
      width="50%"
      @click="editFormVisible = false"
      :close-on-click-modal='false'
      :close-on-press-escape='false'
    >
      <el-form label-width="120px" :model="editForm">
        <el-row>
          <el-col :span="11.5">
            <el-form-item label="二级科室" class="tip">
              <el-select v-model="editForm.sub_departments_id" filterable placeholder="请选择二级科室" style="height:32px" >
                <el-option
                  v-for="item in cacheSubDepartments"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="汇总金额" class="tip">
              <el-input
                type="number"
                size="small"
                v-model="editForm.total_amount"
                placeholder="请输入汇总金额"
              ></el-input>
            </el-form-item>
            <el-form-item label="入库日期" class="tip">
              <el-date-picker
                v-model="editForm.date"
                type="month"
                format="yyyy-MM"
                value-format="yyyy-MM"
                placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button @click="editFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  Apiusedgeneralmaterial,
  ApiusedgeneralmaterialAdd,
  ApiusedgeneralmaterialPut,
  ApiusedgeneralmaterialDel,
} from '@/api'
import { debounce,getNowFormatDate } from "@/utils";
import {mapState} from 'vuex'

export default {
  computed:{
    ...mapState(['cacheSubDepartments'])
  },
  data() {
    return {
      tableData: [],
      editFormVisible: false,
      editForm: {}, //修改
      pageparm:{ 
        page: 1,
        perPage: 20,
        total: null,
        sort_str: null,
        sort_order: null,
      },
      tableLoading: false,
      rules:['date','sub_departments_id','total_amount']
    };
  },
  mounted() {
    this.getList(true)
  },
  methods: {
    handle(index,item,i){
      this.editForm = {...item}
      this.editFormVisible = true
    },
    tableSortChange(column) {
      if (column.order === "descending") {
        this.pageparm.sort_str = column.prop;
        this.pageparm.sort_order = "desc";
      } else if (column.order === "ascending") {
        this.pageparm.sort_str = column.prop;
        this.pageparm.sort_order = "asc";
      }else{
        this.pageparm.sort_str = null;
        this.pageparm.sort_order = null;
      }
      if(column.prop.includes('.')){
        this.pageparm.sort_str = column.prop.split('.')[0] + 's_id'
      }
      this.getList();
    },
    // //搜索
    // search:debounce( function(){
    //   this.pageparm.page = 1
    //   this.getList(); 
    // },800,true),
    // clearSearch: debounce( function(){
    //   this.pageparm.patient = null,
    //   this.pageparm.prescribe_doctor = null,
    //   this.pageparm.execute_doctor = null,
    //   this.pageparm.charging_time = null
    //   this.pageparm.page = 1
    //   this.getList(); 
    // },800,true),
    // dateSearch(){
    //   this.pageparm.page = 1
    //   this.getList();
    // },
    //分页
    callFather(pageparm) {
      this.pageparm.page = pageparm.page;
      this.pageparm.perPage = pageparm.perPage;
      this.getList();
    },
    getList() {
      this.tableLoading = true;
      Apiusedgeneralmaterial(this.pageparm)
        .then(({ data, page }) => {
          this.tableData = data || [];
          this.pageparm.total = page.total;
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
    deleteUser(index, value) {
      this.$confirm("此操作将删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          const loading = this.$loading({
            lock: true,
            text: '删除中...',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          });
          ApiusedgeneralmaterialDel(value.id).then((res) => {
            this.getList(true);
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          }).finally(()=>{
            loading.close()
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    submit: debounce(function () {
      //提交  修改或新增
      const editForm = {...this.editForm};
      for (const key of this.rules) {
        if(!editForm[key] && editForm[key] !== 0){
          this.$message.error("请填写全部信息");
          return
        }
      }
      editForm.material_type = 1
      const loading = this.$loading({
        lock: true,
        text: '加载中...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      if (editForm.id) {
        //有ID就是修改
        ApiusedgeneralmaterialPut(editForm, editForm.id)
          .then((res) => {
            this.$message({
              type: "success",
              message: "修改成功!",
            });
            this.editFormVisible = false;
            this.getList(true);
          })
          .catch(() => {
            this.$message({
              type: "error",
              message: "修改失败!",
            });
          }).finally(()=>{
            loading.close()
          });
      } else {
        ApiusedgeneralmaterialAdd(editForm)
          .then((res) => {
            this.$message({
              type: "success",
              message: "添加成功!",
            });
            this.editFormVisible = false;
            this.getList(true);
          })
          .catch(() => {
            this.$message({
              type: "error",
              message: "添加失败!",
            });
          }).finally(()=>{
            loading.close()
          });
      }
    },500,true),
  },
};
</script>

<style lang='less' scoped>
/deep/ .el-input__inner{
  // width: 180px;
  height: 35px;

}
</style>